/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

using namespace std;

#include <iostream>
#include <cstdlib>
#include "corbaMessageHandler.h"
#include "../tools/log.h"

	
	void CorbaMessageHandler::sendMessage(Message* message)
{
	if(_clients.find(message->getreceiver())!=_clients.end())
	{
	//send locally
	_clients[message->getreceiver()]->insert(message);
	FILE_LOG(logDEBUG)<<"<The Message ["<<message->getTimestamp()<<"|"
		<<(message->isPositive()?"+":"-")<<"|"
		<<(message->getColor()?"w":"r")<<"] "
		<<message<<" is placed in buffer #"<<message->getreceiver()
		<<">"<<endl;
	}
	else
	{
	FILE_LOG(logDEBUG)<<"<The Message ["<<message->getTimestamp()<<"|"
		<<(message->isPositive()?"+":"-")<<"|"
		<<(message->getColor()?"w":"r")<<"] "
		<<message<<" is delivered remotely #"<<message->getreceiver()
		<<">"<<endl;
	

	//remote delivery
	}
}


	void CorbaMessageHandler::receiveMessage(Message* message)
{
	_clients[message->getreceiver()]->insert(message);
	FILE_LOG(logDEBUG)<<"CorbaMessageHandler.receive: received message for LP #"
		<<message->getreceiver()<<endl;
}


	void CorbaMessageHandler::addClient(unsigned int id)
{
	if(_clients.find(id)!=_clients.end())
	{
		FILE_LOG(logWARNING)<<"CorbaMesssageHandler.addClient: Client already exists!"<<endl;
		exit(0);
	}
	else
	{
		_clients[id]=new VectorBuffer();
		FILE_LOG(logDEBUG)<<"vectorbuffer:"<<_clients[id]->isEmpty()<<endl;
		FILE_LOG(logDEBUG)<<"CorbaMessageHandler: Adding New client"<<id<<endl;
	}

}


	bool CorbaMessageHandler::bufferEmpty(unsigned int id)
{
	FILE_LOG(logDEBUG)<<"Bufferempty check"<<_clients[id]->isEmpty()<<endl;
	return _clients[id]->isEmpty();
}

	
	unsigned int CorbaMessageHandler::totalMsgs()
{
	unsigned int nClients=0;
	FILE_LOG(logDEBUG)<<"counting messages"<<_clients.size()<<endl;
	map<unsigned int, VectorBuffer*>::iterator it;
	for (it=_clients.begin();it!=_clients.end();it++)
	{
		
		if (!(*it).second->isEmpty())
			
		nClients+=(*it).second->size();
		
	}
	FILE_LOG(logDEBUG)<<"finished counting messages"<<nClients<<endl;
	return nClients;
	}


	Message* CorbaMessageHandler::readBuffer(unsigned int id)
{
	Message* m=NULL;
	if (_clients.find(id)!=_clients.end())
	{
		FILE_LOG(logDEBUG)<<"Reading first message for Lp#"<<id<<endl;
		if(!_clients[id]->isEmpty())
	 	{
			m=_clients[id]->first();
			FILE_LOG(logDEBUG)<<"got first!"<<endl;
			_clients[id]->remove(m);
		}
		else
		{
			FILE_LOG(logDEBUG)<<"Queue empty!"<<endl;
		}
	}
	else
	{
		FILE_LOG(logERROR)<<"CorbaMessageHandler.readBuffer: Read attempt on an nonexisting buffer!"<<endl;
		exit(0);
	}
	return m;
}

	unsigned int CorbaMessageHandler::bufferSize(unsigned int id)
{
	if (!_clients[id]->isEmpty())
		return _clients[id]->size();
	else return 0;
}